/**
 * 
 */
package net.phoenix.repository;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.jcr.Binary;
import javax.jcr.RepositoryException;

/**
 * @author lixf
 * 
 */
public class InputStreamBinary implements Binary {

	private InputStream is;
	private long size;

	public InputStreamBinary(InputStream is) {
		this.is = is;
		this.size = 0;
	}

	public InputStreamBinary(String str) {
		byte[] bytes;
		try {
			bytes = str.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			bytes = str.getBytes();
		}
		this.is = new ByteArrayInputStream(bytes);

		this.size = bytes.length;
	}

	public InputStreamBinary(InputStream is, int size) {
		this.is = is;
		this.size = size;
	}

	@Override
	public InputStream getStream() throws RepositoryException {
		return this.is;
	}

	@Override
	public int read(byte[] b, long position) throws IOException,
			RepositoryException {
		return this.is.read(b, (int) position, b.length);
	}

	@Override
	public long getSize() throws RepositoryException {
		return this.size;
	}

	@Override
	public void dispose() {

	}

}
